﻿' HtmlWriter.vb
' This class create a html-book. The class is a part of a wordplugin.
' The class HtmlWriter is a class of a htwg-konstanz teamproject
'
' Date:         30/12/2011
' Last edit:    12/07/2012
' author:       Stephan Weidele
' 

Imports System
Imports System.IO
Imports System.Text
Imports System.Drawing


Public Class HtmlWriter

    ' path save the projekt path of the html-side
    Private path As String

    ' imageNumber is a index for the imagename
    Private imageNumber As Integer

    ' Type of the Index. 1 for the first subIndex, 2 for the second subIndex and all other for a normal index
    Private indexType As Integer

    ' pageNumber is a index the pages of the book
    Private pageNumber As Integer

    ' linkNumber is a index the index links of the book
    Private linkNumber As Integer

    ' bookStream save the sourcecode of the index.html
    Private bookStream As System.IO.StreamWriter

    ' bookHtml helps which save a part the sourcecode of index.html. bookHtml will be written in the bookStream
    Private bookHtml As String

    ' indexScriptStream save the sourcecode of the script.js which will use for the index of the book
    Private indexScriptStream As System.IO.StreamWriter

    ' indexScript helps to write sourcecode of the script.js in the indexScriptStream
    Private indexScript As String

    ' indexHtml helps save a part the sourcecode of index.html. bookHtml will be written in the bookStream
    Private indexHtml As String

    ' bookNameString save the name of the project/book
    Private bookNameString As String

    ' utf8 is the encoding 
    Private utf8 As Encoding = New System.Text.UTF8Encoding

    ' newTitle is true if a new title was set in the page
    Private newTitle As Boolean

    ' newTitleString save the text of the new title
    Private newTitleString As String

    ' Constructor of the class
    ' Create the structure of the html project, initialize all variable and create the fist part of the htmlfile
    ' bookName: name of the book
    ' pathString: path of the project
    Sub New(ByVal bookName As String, ByVal pathString As String)

        ' initialize classvariables
        imageNumber = 0
        pageNumber = 1
        linkNumber = 1
        newTitle = False
        Me.bookNameString = bookName

        If (pathString = "") Then
            ' Todo make a exceptionhandling
            pathString = "C:\"

        End If
        Me.path = pathString

        ' Create folders for the project
        System.IO.Directory.CreateDirectory(path + "/" + bookName)
        System.IO.Directory.CreateDirectory(path + "/" + bookName + "/script")
        System.IO.Directory.CreateDirectory(path + "/" + bookName + "/image")

        ' write the first part in the index.html file
        'Me.bookStream = New System.IO.StreamWriter(Me.path + bookName + "/index.html")
        Me.bookStream = New System.IO.StreamWriter(Me.path + bookNameString + "/index.html", False, utf8)
        Me.bookStream.Write("<!DOCTYPE html><html><head><title>" + bookName + "</title><meta charset=" + Chr(34) + "UTF-8" + Chr(34) + ">")
        Me.bookStream.Write("<script src=" + Chr(34) + "http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" + Chr(34) + "type=" + Chr(34) + "text/javascript" + Chr(34) + "></script>")
        Me.bookStream.Write("<script src=" + Chr(34) + "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" + Chr(34) + "type=" + Chr(34) + "text/javascript" + Chr(34) + "></script>")

        ' global access
        Me.bookStream.Write("<script src=" + Chr(34) + "https://word-book-plugin.googlecode.com/svn/trunk/EBookGen/files/booklet/jquery.easing.1.3.js" + Chr(34) + "type=" + Chr(34) + "text/javascript" + Chr(34) + "></script>")
        Me.bookStream.Write("<script src=" + Chr(34) + "https://word-book-plugin.googlecode.com/svn/trunk/EBookGen/files/booklet/jquery.booklet.1.2.0.min.js" + Chr(34) + "type=" + Chr(34) + "text/javascript" + Chr(34) + "></script>")

        ' ToDo: make access to googlecode and delte panthes url
        'Me.bookStream.Write("<link href=" + Chr(34) + "http://word-book-plugin.googlecode.com/svn/trunk/EBookGen/files/booklet/jquery.booklet.1.2.0.css" + Chr(34) + " type=" + Chr(34) + "text/css" + Chr(34) + " rel=" + Chr(34) + "stylesheet" + Chr(34) + " media=" + Chr(34) + "screen, projection, tv" + Chr(34) + "/>")
        Me.bookStream.Write("<link href=" + Chr(34) + "http://www.panthes.de/teamprojekt/booklet/jquery.booklet.1.2.0.css" + Chr(34) + " type=" + Chr(34) + "text/css" + Chr(34) + " rel=" + Chr(34) + "stylesheet" + Chr(34) + " media=" + Chr(34) + "screen, projection, tv" + Chr(34) + "/>")

        Me.bookStream.Write("<script src=" + Chr(34) + "script/index.js" + Chr(34) + "type=" + Chr(34) + "text/javascript" + Chr(34) + "></script>")
        Me.bookStream.Write("<script type=" + Chr(34) + "text/javascript" + Chr(34) + ">$(function() {$('#mybook').booklet({manual:   false,overlays: true, hovers:   true, width:  900, height: 600 });});</script>")
        Me.bookStream.Write("<style type=" + Chr(34) + "text/css" + Chr(34) + "> body { background-image:url(https://word-book-plugin.googlecode.com/svn/trunk/EBookGen/files/images/backroundPicture.jpg); background-position:50% 45%;} </style>")
        Me.bookStream.Write("</head> <body style=" + Chr(34) + "position:relative; top:83px" + Chr(34) + "; > <header> </header> <nav> </nav> <section > <div id=" + Chr(34) + "mybook" + Chr(34) + "> <div class=" + Chr(34) + "b-load" + Chr(34) + ">")
        Me.bookStream.Close()
        Me.bookStream.Dispose()

        ' initialize the strings which will be written in the streams and then in the files
        indexScript = "$(function() {"
        indexHtml = "<div> <h2>Inhaltsverzeichnis</h2>"
        bookHtml = ""

    End Sub

    ' Create a new index title in the book
    ' The function write a title in the html-file and create a index title (a link at the first page to current page). With the help of the function newTitleatTheSameSide() will be created the index in the book.
    ' textString: text of the title
    ' sizeValue: font size
    ' colorValue: font color
    ' textStyle: style of lettering
    ' boldStyle: bold letters
    ' cursiveStyle: cursive letters
    ' underlineStyle: underlined letters
    ' indexTypeValue: type of index (1 = first subindex, 2 ? second subindex, 0 = normal index)
    Public Sub writeTitle(ByVal textString As String, ByVal sizeValue As Integer, ByVal colorValue As String, ByVal textStyle As String, ByVal boldStyle As Boolean, ByVal cursiveStyle As Boolean, ByVal underlineStyle As Boolean, ByVal indexTypeValue As Integer)

        indexType = indexTypeValue

        If (boldStyle) Then
            bookHtml = bookHtml + "<b>"
        End If

        If (cursiveStyle) Then
            bookHtml = bookHtml + "<i>"
        End If

        If (underlineStyle) Then
            bookHtml = bookHtml + "<u>"
        End If

        ' write the text into the html-string
        bookHtml = bookHtml + "<font size=" + Chr(34) + sizeValue.ToString + " pt" + Chr(34) + "style = " + Chr(34) + "font-family: " + textStyle + Chr(34) + " color=" + Chr(34) + colorValue + Chr(34) + ">" + textString + "</font>"

        If (underlineStyle) Then
            bookHtml = bookHtml + "</u>"
        End If

        If (cursiveStyle) Then
            bookHtml = bookHtml + "</i>"
        End If

        If (boldStyle) Then
            bookHtml = bookHtml + "</b>"
        End If

        newTitleString = newTitleString + textString
        newTitle = True

    End Sub




    ' newTitleatTheSameSide helps the create a new title
    ' This function write the title in the book. the function will be invoked at the end of every page. If you want more then one title on one page you must invoke the funkction manually
    Public Sub newTitleatTheSameSide()

        ' open the supportfunction writeIndex to write the index in the string and reset the newTitle function
        If (newTitle) Then

            writeIndex(newTitleString)

            newTitleString = ""
            newTitle = False

            indexType = 0

        End If

    End Sub


    ' Create a new text in the book
    ' The function write a textpart in the html-file
    ' textString: text of the title
    ' sizeValue: font size
    ' colorValue: font color
    ' textStyle: style of lettering
    ' boldStyle: bold letters
    ' cursiveStyle: cursive letters
    ' underlineStyle: underlined letters
    Public Sub writeText(ByVal textString As String, ByVal sizeValue As Integer, ByVal colorValue As String, ByVal textStyle As String, ByVal boldStyle As Boolean, ByVal cursiveStyle As Boolean, ByVal underlineStyle As Boolean)

        If (boldStyle) Then
            bookHtml = bookHtml + "<b>"
        End If

        If (cursiveStyle) Then
            bookHtml = bookHtml + "<i>"
        End If

        If (underlineStyle) Then
            bookHtml = bookHtml + "<u>"
        End If

        ' write the text into the html-string
        bookHtml = bookHtml + "<font size=" + Chr(34) + sizeValue.ToString + " pt" + Chr(34) + "style = " + Chr(34) + "font-family: " + textStyle + Chr(34) + " color=" + Chr(34) + colorValue + Chr(34) + ">" + textString + "</font>"

        If (underlineStyle) Then
            bookHtml = bookHtml + "</u>"
        End If

        If (cursiveStyle) Then
            bookHtml = bookHtml + "</i>"
        End If

        If (boldStyle) Then
            bookHtml = bookHtml + "</b>"
        End If
    End Sub


    ' Function to create a linebreak
    Public Sub writeLineBrake()

        bookHtml = bookHtml + "<br>"

    End Sub

    ' Function to write a tap (six space characters)
    Public Sub writeTab()

        bookHtml = bookHtml + "&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;"

    End Sub

    ' Function to write one singel space character
    Public Sub writeSpace()

        bookHtml = bookHtml + "&ensp;"

    End Sub

    ' Function to write a blank line
    Public Sub writeBlankLine()

        bookHtml = bookHtml + "<br></br>"

    End Sub

    ' Function to paint a picture
    ' The Function copy the picture into the image folder of the html project. Then the function write the imagetag into the bookhtml-string. This function overwrite text and other pictures, use this function only if you're no other options.
    ' imagepath: path of the image
    ' width: width of the image
    ' height: hight of the image
    ' topValue: size between the top and the image
    ' leftValue: size between the left side and the image
    Public Sub setImage(ByVal imagepath As String, ByVal width As Integer, ByVal height As Integer, ByVal topValue As Integer, ByVal leftValue As Integer)

        ' copy the picture into the html image folder
        Dim image1 As Image = Image.FromFile(imagepath)
        Try
            image1.Save(Me.path + bookNameString + "\image\image" + imageNumber.ToString + ".jpg")
        Catch ex As Exception
            ' Todo make a exceptionhandling
            Dim fehler As String = ex.ToString
        End Try

        ' write the picture into the html-string
        bookHtml = bookHtml + "<div  style=" + Chr(34) + "position:absolute; top:" + topValue.ToString + "px; left:" + leftValue.ToString + "px; " + Chr(34) + ">"
        bookHtml = bookHtml + "<img width=" + Chr(34) + width.ToString + "px" + Chr(34) + " height=" + Chr(34) + height.ToString + "px" + Chr(34) + " alt= " + Chr(34) + Chr(34) + " src=" + Chr(34) + "image/image" + imageNumber.ToString + ".jpg" + Chr(34) + ">"
        bookHtml = bookHtml + "</div>"

        ' increase numbers of images
        imageNumber = imageNumber + 1

    End Sub

    ' Function to paint a picture with text
    ' The Function copy the picture into the image folder of the html project. Then the function write the imagetag into the bookhtml-string. This function set the image and the text in an position which the user can set.
    ' imagepath: path of the image
    ' width: width of the image
    ' height: hight of the image
    ' textPosition: position of the text who follows the image
    ' horizontalSpace: horizintal space between text and the image
    ' verticalSpace: vertical space between text and the iamge
    Public Sub setImageWithText(ByVal imagepath As String, ByVal width As Integer, ByVal height As Integer, ByVal textPosition As String, ByVal horizontalSpace As Integer, ByVal verticalSpace As Integer)

        ' if the parameter textPosition not known choose a default parameter
        If (String.Compare(textPosition, "left") <> 0) And (String.Compare(textPosition, "right") <> 0) And (String.Compare(textPosition, "texttop") <> 0) And (String.Compare(textPosition, "absmiddle") <> 0) And (String.Compare(textPosition, "absbottom") <> 0) And (String.Compare(textPosition, "baseline") <> 0) And (String.Compare(textPosition, "bottom") <> 0) And (String.Compare(textPosition, "middle") <> 0) And (String.Compare(textPosition, "top") <> 0) Then
            ' Todo make a exceptionhandling
            textPosition = "right"

        End If

        ' copy the picture into the html image folder
        Dim image1 As Image = Image.FromFile(imagepath)
        Try
            image1.Save(Me.path + bookNameString + "\image\image" + imageNumber.ToString + ".jpg")
        Catch ex As Exception
            ' Todo make a exceptionhandling
            Dim fehler As String = ex.ToString

        End Try

        ' write the picture into the html-string
        bookHtml = bookHtml + "<img width=" + Chr(34) + width.ToString + "px" + Chr(34) + " height=" + Chr(34) + height.ToString + "px" + Chr(34) + " alt= " + Chr(34) + Chr(34) + " src=" + Chr(34) + "image/image" + imageNumber.ToString + ".jpg" + Chr(34) + "align=" + Chr(34) + textPosition + Chr(34) + "vspace=" + Chr(34) + verticalSpace.ToString + Chr(34) + " hspace=" + Chr(34) + horizontalSpace.ToString + Chr(34) + ">"

        ' increase numbers of images
        imageNumber = imageNumber + 1

    End Sub

    ' Function to write on a new page
    Public Sub nextPage()

        ' open the support function to write the last title of this page
        newTitleatTheSameSide()

        ' increase pagenumer
        pageNumber = pageNumber + 1

        ' write a endtag and a newtag in the html file to create a new file
        bookHtml = bookHtml + "</div><div>"

    End Sub

    ' Support function to write index or sub index into the Script string and the html string
    ' textString: string which will write in the index
    Private Sub writeIndex(ByVal textString As String)

        ' write the an index into the script-string
        indexScript = indexScript + "$('#custom-link-" + linkNumber.ToString + "').click(function(e){ e.preventDefault(); $('#mybook').booklet( " + pageNumber.ToString + ");  });"

        If (indexType = 1) Then

            ' write the first subindex into the html-string
            indexHtml = indexHtml + "&ensp;&ensp;&ensp;" + "<a id=" + Chr(34) + "custom-link-" + linkNumber.ToString + Chr(34) + " href= " + Chr(34) + "#" + Chr(34) + ">" + textString + "</a> <br>"

        ElseIf (indexType = 2) Then

            ' write the second subindex into the html-string
            indexHtml = indexHtml + "&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;" + "<a id=" + Chr(34) + "custom-link-" + linkNumber.ToString + Chr(34) + " href= " + Chr(34) + "#" + Chr(34) + ">" + textString + "</a> <br>"

        Else

            ' write a normal index into the html-string
            indexHtml = indexHtml + "<a id=" + Chr(34) + "custom-link-" + linkNumber.ToString + Chr(34) + " href= " + Chr(34) + "#" + Chr(34) + ">" + textString + "</a> <br>"

        End If

        linkNumber = linkNumber + 1

    End Sub

    ' Function to finish the html project
    ' Finished all tags and write the streams in the files
    Public Sub finishBook()

        ' finished indexScript and indexHtml
        indexScript = indexScript + "});"
        indexHtml = indexHtml + "</div>"

        ' open the file and write the string into it with support of a stream
        Me.bookStream = New System.IO.StreamWriter(Me.path + bookNameString + "/index.html", True, utf8)
        Me.bookStream.Write(indexHtml)
        Me.bookStream.Write("<div>" + bookHtml + "</div> </div> </div> </section> </body> </html>")
        Dim vallue As Encoding = Me.bookStream.Encoding
        Me.bookStream.Close()
        Me.bookStream.Dispose()

        ' open the file and write the string into it with support of a stream
        Me.indexScriptStream = New System.IO.StreamWriter(Me.path + bookNameString + "/script/index.js")
        Me.indexScriptStream.Write(indexScript)
        Me.indexScriptStream.Close()
        Me.indexScriptStream.Dispose()

    End Sub

End Class

